const express = require("express")
// const mysql = require("./db")
const {graphqlHTTP} = require('express-graphql');
const GraphQLSchema = require('./schema/default.js');
const loggingMiddleware = require('./middlewars/loginCheck')
const loginCheckMiddleware = require('./middlewars/loginCheck')
const cors = require('cors')
const router = express.Router()

const app = express()
// 处理跨域请求
app.use(cors());
// 处理请求
app.use(express.json());//express.json=bodyParser.json
app.use(express.urlencoded({extended: true}));

// 使用登录中间件
// app.use(loggingMiddleware);
// router.post('/login', (req,res) => {
// console.log("==== 请求过来了 ====");
// }, loginCheckMiddleware);

app.use('/graphql', graphqlHTTP((request) => {
  return {
    schema: GraphQLSchema,
    graphiql: true,
    rootValue: request.headers,
    context: request.headers
  }
}))

// 配置express中间件
// app.use('/graphql', graphqlHTTP({
//     schema: GraphQLSchema, // MyGraphQLSchema待定
//     graphiql: true, // 开发环境打开调试面板，生产的时候可以关闭
//   }),
// );

app.listen(3000, () => {
  console.log("服务已启动: localhost:3000");
})
